<?php
require(dirname(__FILE__)."/includes/init.php");
 $flag = "";
 $up = "";
 $flag = isset($_REQUEST['flag'])?$_REQUEST['flag']:'2';
 if($flag==1){
	      $user = $_REQUEST['user_name'];
	      $pwd = $_REQUEST['user_pwd'];
	      $v = new verify($user,$pwd);
	      $v->verifyUser();	      
?>
<?php }elseif ($flag==2 && !empty($_SESSION['user_name'])){
	      $title = isset($_REQUEST['title'])?addslashes($_REQUEST['title']):'';
	      $type = isset($_REQUEST['type'])?$_REQUEST['type']:'';
	      $content = isset($_REQUEST['content'])?addslashes($_REQUEST['content']):'';

		  if (!empty($title) && !empty($content)) {
		  $conArr = explode(" ", $content);
		  $num = count($conArr);
		  $rnum = rand(0,$num);
		  $conArr[$rnum] = $conArr[$rnum]." |*TRACK*| ";
		  $content = '';
		  for($i=0;$i<$num;$i++){
		      $content = $content." ".$conArr[$i];
		  }

		      $sql = "insert into template(title, content, timestamp, flag, type) values('".$title."','".$content."','".time()."',0,'".$type."')";
		#	echo $sql;exit;
		      if($GLOBALS['db']->dbquery($sql)){
			      //获得刚刚添加的模板的ID
			      $sql = "select max(id) as num from template";
				  $rowResult = $GLOBALS['db']->dbGetRow($sql);
				  //分别给第三方的和自己的SMTP指定模板
				  if(in_array($type, array(10,20,30,40,50))){
				    //只要emailLimitTypeArr这个数组中的就OK,自己的配置的SMTP发送表.
				  	insertIntoNum($emailLimitTypeArr, $rowResult['num']);
				  }
				  if(in_array($type, array(60,70,80,90))){
				  	insertIntoThirdPartNum($emailThirdTypeArr, $rowResult['num']);
				  }
				  if(in_array($type,array(100,110,120,130))){
				  	insertIntoThirdPartNum($emailThirdTypeArrB, $rowResult['num']);
				  }
				  //分别给第三方的和自己的SMTP指定模板
		          echo "<script>alert('update success!');history.go(-1);</script>";exit;
		      }else{
		          echo "failed";exit;
		      }
		   } 
?>
<?php }else{?>
		  hello
<?php }?>
<title>添加模板</title>
<?php if(empty($_SESSION['user_name'])){?>
<form action="" name="verify" method="POST"> 
	<table width="854" height="292" border="1">
	  <tr>
	    <td>Name:</td>
	    <td><input type="text" name="user_name" /></td>
	  </tr>
	  <tr>
	    <td>Content:</td>
	    <td><input type="text" name="user_pwd" /></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td><input type="hidden" name="flag" value="1"><input type="submit" name="Submit" value="submit" /></td>
	  </tr>
	</table>
</form>
<?php }else{?>
<form action="" name="update" method="POST"> 
 <table width="854" height="292" border="1">
  <tr>
    <td>Name:</td>
    <td><input type="text" name="title" /></td>
  </tr>
          <tr>
            <td>Type:</td>
            <td>
               <p>
                 <input type="text" name="type" />
                 <br/>
           10--50;供自身SMTP使用;20,为常用的编号,10,30,40,50为保留编号<br/>
           其它的供第三方SMTP使用;60,为常用编号,70,80,90为保留编号</p>
            </td>
          </tr>
  <tr>
    <td>Content:</td>
    <td><textarea name="content" cols="150" rows="30"></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="hidden" name="flag" value="2"><input type="submit" name="Submit" value="submit" />
      <a href="list.php" target=_blank>list</a>
      <a href="checkInfo.php" target=_blank>checkInfo</a>
</td>
  </tr>
</table>
</form>
<?php } ?>